package com.trust.pams.util;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class SvnUtil {
    //svn连接地址
    public static String storeUrl=null;
    private static final String USERNAME = "zhao.liu";
    private static final String PASSWD = "lz53298742";


    static{
        storeUrl =  "http://139.129.221.62:88/svn/";
    }
    /**
     * @Author: winterchen
     * @Description: 获取SVNRepository
     * @Date: 2018/2/6
     * @param url
     */
    public static SVNRepository getSVNRepository(String url) throws SVNException{

        SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        ISVNAuthenticationManager authManager =  SVNWCUtil.createDefaultAuthenticationManager( USERNAME , PASSWD.toCharArray());
        repository.setAuthenticationManager( authManager );
        return repository;
    }

    /**
     * @Author: winterchen
     * @Description: 获取SVNRepository
     * @Date: 2018/2/7
     * @param
     */
    public static SVNRepository getSVNRepository() throws SVNException {
        return SvnUtil.getSVNRepository(storeUrl);
    }


    /**
     * @Author: winterchen
     * @Description: 获取 SVNUpdateClient
     * @Date: 2018/2/9
     * @param
     */
    public static SVNUpdateClient getSVNUpdateClient(){

        //声明SVN客户端管理类
        SVNClientManager ourClientManager = null;

        //初始化库。 必须先执行此操作。具体操作封装在setupLibrary方法中。
        setupLibrary();



        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        //实例化客户端管理类
        ourClientManager = SVNClientManager.newInstance(
                (DefaultSVNOptions) options, USERNAME, PASSWD);

        //通过客户端管理类获得updateClient类的实例。
        SVNUpdateClient updateClient = ourClientManager.getUpdateClient();

        updateClient.setIgnoreExternals(false);

        return updateClient;

    }

    /*
     * 初始化库
     */
    private static void setupLibrary() {
        /*
         * For using over http:// and https://
         */
        DAVRepositoryFactory.setup();
        /*
         * For using over svn:// and svn+xxx://
         */
        SVNRepositoryFactoryImpl.setup();

        /*
         * For using over file:///
         */
        FSRepositoryFactory.setup();
    }

    /**
     * @Author: winterchen
     * @Description: 获取 SVNClientManager
     * @Date: 2018/2/9
     * @param
     */
    public static SVNClientManager getSVNClientManager(){
        DAVRepositoryFactory.setup();
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        //实例化客户端管理类
        return SVNClientManager.newInstance((DefaultSVNOptions) options, USERNAME, PASSWD);
    }

}